(function($) {
	var type = ""
	var sourceData
	$.init();
	$.plusReady(function() {
		baseSetup();
	});
	if (!mui.os.plus) {
		baseSetup()
	}
	//获取信息
	function getPersonTrainData(){
		var parr = sourceData['subID']
		var pdict = {'rw':parr}
		//console.log(JSON.stringify(pdict))
		var paramDict = {'method':'web.INMTeaComm:getDrillReleaseSub','params':JSON.stringify(pdict),'type':'RecMethod'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result.msgcode != '1') {
				updateUI()
				return
			}
			updateUI(result.data)
		}, function(errorStr) {
			//mui.toast('请重试')
			updateUI()
		});
	}
	// function getPersonTrainData(){
	// 	clearUI()
	// 	setTimeout(function(){
	// 		$("#reasonArea")[0].style.display = 'none';
	// 		if (type != 'look') {
	// 			document.body.classList.remove("look")
	// 		}
	// 		updateUI()
	// 	},1000)
		
	// }
	function updateUI(dataDict) {
		if (!dataDict) {
			dataDict = sourceData
		}
		$('#classTitle')[0].innerText = sourceData['ReleaseDesc']
		$('#nurName')[0].innerText = sourceData['perName']
		$('#nurNo')[0].innerText = sourceData['perNo']
		$('#nurWard')[0].innerText = sourceData['wardDesc']
		$('#wrapper')[0].style.top = ($('#header')[0].clientHeight + 15) + 'px'
		var trs = document.querySelectorAll('tr')
		if (sourceData['ReleaseWay'] != 'O') { //线下
			var sign = ''
			var SignStartTime = sourceData.SignStartTime||''
			var SignEndTime = sourceData.SignEndTime||''
			if ( SignStartTime != '') {
				sign = '签到'
			}else if (SignEndTime != '') {
				if (sign != '') {
					sign += ','
				}
				sign += '签退'
			}
			trs[0].innerHTML = '<td>考勤信息</td><td>'+sign+'</td><td>'+sourceData['UseStatusDesc1']+'</td>'
			trs[1].style.display = 'none'
		}else{
			trs[0].innerHTML = '<td>课件完成进度</td><td>'+sourceData['videoTimeRate']+'</td><td>'+sourceData['UseStatusDesc2']+'</td>'
			trs[1].innerHTML = '<td>视频插题</td><td>'+sourceData['UseStatusDesc3']+'</td><td>'+sourceData['UseStatusDesc3']+'</td>'
		}
		trs[2].innerHTML = '<td>随堂练习</td><td>'+sourceData['UseClassScore']+'</td><td>'+sourceData['UseStatusDesc4']+'</td>'
		trs[3].innerHTML = '<td>问卷调查</td><td>'+sourceData['DrillUseSurveyStatus']+'</td><td>'+sourceData['UseStatusDesc5']+'</td>'
		trs[4].innerHTML = '<td>结课考核</td><td>'+sourceData['UseResultScore']+'</td><td>'+sourceData['UseStatusDesc6']+'</td>'
		trs[5].innerHTML = '<td>课后作业</td><td>'+sourceData['UseHomeworkScore']+'</td><td>'+sourceData['UseStatusDesc7']+'</td>'
		trs[6].innerHTML = '<td>课程学分</td><td colspan="2">'+sourceData['DrillCredit']+'</td>'
		trs[7].innerHTML= '<td>获得学分</td><td colspan="2">'+dataDict['UseDrillScore']+'</td>'
		trs[8].innerHTML = '<td>获得学分</td><td colspan="2"><div class="mui-input-row">'+
			'<input type="text" id="userScore" class="mui-input-clear" placeholder="输入学分" value="'+dataDict['UseDrillScore']+'"></div></td>'
		trs[10].innerHTML= '<td>课程结果</td><td colspan="2" class="statusText">'+sourceData['DrillResultDesc']+'</td>'
	}
	function saveTeachSub(){
		var input = $('#userScore')[0]
		input.blur()
		var valueStr = input.value
		if (valueStr == "") {
			mui.toast('学分不能为空')
			return
		}
		var valueStr = dealInputValue(valueStr)
		var flag = this.getAttribute('data-id')
		var textStr = this.innerText
		var parr = sourceData['subID']+'「'+valueStr+'「'+flag;
		var pdict = {'parr':parr,'activeName':3}
		//console.log(JSON.stringify(pdict))
		var paramDict = {'method':'web.INMTeaComm:SaveTeachSub','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result['msgcode'] != '1') {
				mui.toast(result.msg)
				return
			}
			mui.toast(textStr+'成功')
			
			if (flag == 'Y') {
				document.body.classList.add("look")
			}
		}, function(errorStr) {
			mui.toast(errorStr)
		});
	}
	function saveSubmitBtnTapped(){
		//SaveTeachSub
		if (type == 'give') {
			if ($("#trGive input")[0].value == '') {
				mui.toast('请录入学分')
				return
			}
			$("#trGive input")[0].blur()
		}else if ($("#changeBtn")[0].innerText == '') {
			mui.toast('请录入学分')
			return
		}
		document.body.classList.add("look")
		if (this.getAttribute('class').indexOf('saveBtn')!= -1) {
			mui.toast('保存成功<br/>获取下一个学员信息')
		}else{
			mui.toast('提交成功<br/>获取下一个学员信息')
		}
		getPersonTrainData()
	}
	function baseSetup() {
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview()
		sourceData = self.sourceData;
		//console.log(JSON.stringify(sourceData))
		type = self.pageType;
		document.body.classList.add(type)
		getPersonTrainData()
		if (type == 'look') {
			return
		}
		mui('body').on('input','#userScore',function(){
			dealInputValue(this.value)
		}) 
		//保存
		$('.btnArea').on('tap', '.saveBtn', saveTeachSub);
		//提交
		$('.btnArea').on('tap', '.submitBtn', saveTeachSub);
		if (type == 'give') {
			return
		}
		//更改学分
		$('body').on('tap', '.changeBtn', function() {
			if (document.body.getAttribute('class').indexOf('look')!= -1) {
				return;
			}
			$('#changePopo').popover('toggle');
		});
		$('#changePopo').on('tap', '.sureBtn', function() {
			if ($("#changePopo input")[0].value=="") {
				mui.toast('请输入学分')
				return;
			}
			if ($("#reason")[0].value=="") {
				mui.toast('请输入更改原因')
				return;
			}
			$("#changeBtn")[0].innerText = '3';
			$("#reasonArea")[0].style.display = 'block';
			$('.mui-scroll-wrapper').scroll().reLayout();
			$("#changePopo input")[0].blur()
			$("#reason")[0].blur()
			$('#changePopo').popover('toggle');
		});
		$('#changePopo').on('tap', '.closeBtn', function() {
			$("#changePopo input")[0].blur()
			$("#reason")[0].blur()
			$('#changePopo').popover('toggle');
		});
	}
	function clearUI(){
		$('#nurName')[0].innerText = ''
		$('#nurNo')[0].innerText = ''
		$('#nurWard')[0].innerText = ''
		var trs = document.querySelectorAll('tr')
		trs[0].innerHTML = '<td>课件完成进度</td><td></td><td></td>'
		trs[1].innerHTML = '<td>视频插题</td><td></td><td></td>'
		trs[2].innerHTML= '<td>随堂练习</td><td></td><td></td>'
		trs[3].innerHTML= '<td>结课考核</td><td></td><td></td>'
		trs[4].innerHTML= '<td>课后作业</td><td></td><td></td>'
		trs[5].innerHTML= '<td>问卷调查</td><td></td><td></td>'
		trs[6].innerHTML= '<td>课程学分</td><td></td><td></td>'
		trs[7].innerHTML= '<td>获得学分</td><td colspan="2"></td>'
		trs[8].innerHTML= '<td>获得学分</td><td colspan="2"></td>'
		$('#trChange .changeBtn')[0].innerHTML = ''
		trs[10].innerHTML= '<td>课程结果</td><td colspan="2"></td>'
	}
	function dealInputValue(valueStr){
		//console.log(valueStr)
		valueStr = valueStr.replace(/[^0-9\\.]/g,"")
		if (parseFloat(valueStr) > parseFloat(sourceData['DrillCredit'])) {
			valueStr = sourceData['DrillCredit']
		}
		if (valueStr.indexOf('.') != -1) {
			var arr = valueStr.split('.')
			if (arr.length > 2) {
				valueStr = arr[0]+'.'+arr[1]
			}else if (arr[1].length > 2){
				valueStr = arr[0]+'.'+arr[1].substring(0,2)
				mui.toast('最多两位小数')
			}
		}
		$('#userScore')[0].value = valueStr
		return valueStr
	}
})(mui);
//保存/提交后需要切换下一个人
